package com.ecarx.vhaltests.hy11.testcases.keyinput;

import android.car.VehicleAreaType;
import android.car.VehiclePropertyIds;
import android.util.Log;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VhalKeyInputUnitTestBase;

import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HwKeyInputOperTest extends VhalKeyInputUnitTestBase {
    private static final String TAG = "HwKey InputOperTest";

    private static int propertyId = VehiclePropertyIds.HW_KEY_INPUT;

    private static final int AREA_ID = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;

    private static IpcpClient ipcpClient;

    int delayTime = 500;

    @Test
    public void test001_KeyInput() {
        Log.d(TAG, "test001_KeyInput_HardKeyTop");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        delay(delayTime);
        int[] inputEventPress = {3,0,0,0,0,0,0,0,0};
        mockKeyInputEvent(inputEventPress);
        delay(delayTime);
        int[] inputEventRelease = {4,0,0,0,0,0,0,0,0};
        mockKeyInputEvent(inputEventRelease);
    }

    @Test
    public void test002_KeyInput() {
        Log.d(TAG, "test002_KeyInput_HardKeyBottom");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        delay(delayTime);
        int[] inputEventPress = {0,3,0,0,0,0,0,0,0};
        mockKeyInputEvent(inputEventPress);
        delay(delayTime);
        int[] inputEventRelease = {0,4,0,0,0,0,0,0,0};
        mockKeyInputEvent(inputEventRelease);
    }

    @Test
    public void test003_KeyInput() {
        Log.d(TAG, "test003_KeyInput_HardKeyLeft");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        delay(delayTime);
        int[] inputEventPress = {0,0,3,0,0,0,0,0,0};
        mockKeyInputEvent(inputEventPress);
        delay(delayTime);
        int[] inputEventRelease = {0,0,4,0,0,0,0,0,0};
        mockKeyInputEvent(inputEventRelease);
    }

    @Test
    public void test004_KeyInput() {
        Log.d(TAG, "test004_KeyInput_HardKeyRight");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        delay(delayTime);
        int[] inputEventPress = {0,0,0,3,0,0,0,0,0};
        mockKeyInputEvent(inputEventPress);
        delay(delayTime);
        int[] inputEventRelease = {0,0,0,4,0,0,0,0,0};
        mockKeyInputEvent(inputEventRelease);
    }

    @Test
    public void test005_KeyInput() {
        Log.d(TAG, "test005_KeyInput_HardKeyConfirm");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        delay(delayTime);
        int[] inputEventPress = {0,0,0,0,3,0,0,0,0};
        mockKeyInputEvent(inputEventPress);
        delay(delayTime);
        int[] inputEventRelease = {0,0,0,0,4,0,0,0,0};
        mockKeyInputEvent(inputEventRelease);
    }

    @Test
    public void test006_KeyInput() {
        Log.d(TAG, "test006_KeyInput_HardKeyVR");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        delay(delayTime);
        int[] inputEventPress = {0,0,0,0,0,3,0,0,0};
        mockKeyInputEvent(inputEventPress);
        delay(delayTime);
        int[] inputEventRelease = {0,0,0,0,0,4,0,0,0};
        mockKeyInputEvent(inputEventRelease);
    }

    @Test
    public void test007_KeyInput() {
        Log.d(TAG, "test007_KeyInput_HardKeyCustom");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        delay(delayTime);
        int[] inputEventPress = {0,0,0,0,0,0,3,0,0};
        mockKeyInputEvent(inputEventPress);
        delay(delayTime);
        int[] inputEventRelease = {0,0,0,0,0,0,4,0,0};
        mockKeyInputEvent(inputEventRelease);
    }

    @Test
    public void test008_KeyInput() {
        Log.d(TAG, "test008_KeyInput_HardKeyMenu");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        delay(delayTime);
        int[] inputEventPress = {0,0,0,0,0,0,0,3,0};
        mockKeyInputEvent(inputEventPress);
        delay(delayTime);
        int[] inputEventRelease = {0,0,0,0,0,0,0,4,0};
        mockKeyInputEvent(inputEventRelease);
    }

    @Test
    public void test009_KeyInput() {
        Log.d(TAG, "test008_KeyInput_HardKeyMute");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        delay(delayTime);
        int[] inputEventPress = {0,0,0,0,0,0,0,0,3};
        mockKeyInputEvent(inputEventPress);
        delay(delayTime);
        int[] inputEventRelease = {0,0,0,0,0,0,0,0,4};
        mockKeyInputEvent(inputEventRelease);
    }

    @Test
    public void test010_KeyInput() {
        Log.d(TAG, "test010_KeyInput_HardKeyDriMode");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        int[] inputCcsmEventNor = {0,0,0};
        mockCCSMKeyInputEvent(inputCcsmEventNor);
        delay(delayTime);
        int[] inputCcsmEventPress = {1,0,0};
        mockCCSMKeyInputEvent(inputCcsmEventPress);
        delay(delayTime);
        mockCCSMKeyInputEvent(inputCcsmEventNor);
    }

    @Test
    public void test011_KeyInput() {
        Log.d(TAG, "test011_KeyInput_HardKeyCusKeyBut");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        int[] inputCcsmEventNor = {0,0,0};
        mockCCSMKeyInputEvent(inputCcsmEventNor);
        delay(delayTime);
        int[] inputCcsmEventPress = {0,1,0};
        mockCCSMKeyInputEvent(inputCcsmEventPress);
        delay(delayTime);
        mockCCSMKeyInputEvent(inputCcsmEventNor);
    }

    @Test
    public void test012_KeyInput() {
        Log.d(TAG, "test012_KeyInput_HardKeyCsdPowBut");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        int[] inputCcsmEventNor = {0,0,0};
        mockCCSMKeyInputEvent(inputCcsmEventNor);
        delay(delayTime);
        int[] inputCcsmEventPress = {0,0,1};
        mockCCSMKeyInputEvent(inputCcsmEventPress);
        delay(delayTime);
        mockCCSMKeyInputEvent(inputCcsmEventNor);
    }

    @Test
    public void test013_KeyInput() {
        Log.d(TAG, "test013_KeyInput_HardKeyRaceMode");
        delay(delayTime);
        int[] inputEventNor = {0,0,0,0,0,0,0,0,0};
        //按键状态  0:Normal  1:Short_click   2:Long_click  3:Press  4:Release
        //对应位置{Top,Bottom,Left,Right,Confirm,VR,Custom,Menu,Mute}
        mockKeyInputEvent(inputEventNor);
        int[] inputCcsmEventNor = {0,0,0};
        mockCCSMKeyInputEvent(inputCcsmEventNor);
        delay(delayTime);
        int[] inputSwsmEventNor = {0};
        mockSWSMKeyInputEvent(inputSwsmEventNor);
        delay(delayTime);
        int[] inputSwsmEventPress = {1};
        mockSWSMKeyInputEvent(inputSwsmEventPress);
        delay(delayTime);
        mockSWSMKeyInputEvent(inputSwsmEventNor);
    }

    private static void mockKeyInputEvent(int[] value) {
        int svcId = 0x127;  //dec:295
        int operId = 0x201a; //dec:8218
        int[] operIds = {operId};
        if (ipcpClient == null) {
            ipcpClient = new IpcpClient();
            ipcpClient.setConfig(svcId, operIds);
        }
        boolean result = ipcpClient.setDimHUDSignal(svcId, operId, new String[]{"WheelKeyEventUser"}, value);
        Assert.assertTrue(result);
    }

    private static void mockCCSMKeyInputEvent(int[] value) {
        int svcId = 30;
        int operId = 999;
        int[] operIds = {operId};
        if (ipcpClient == null) {
            ipcpClient = new IpcpClient();
        }
        ipcpClient.setConfig(svcId, operIds);
        String[] inputCcsmKey = {"driModBut","cusKeyBut","csdPowBut"};
        boolean result = ipcpClient.setCCSMSignal(svcId, operId, inputCcsmKey, value);
        Assert.assertTrue(result);
    }

    private static void mockSWSMKeyInputEvent(int[] value) {
        int svcId = 38;
        int operId = 999;
        int[] operIds = {operId};
        if (ipcpClient == null) {
            ipcpClient = new IpcpClient();
        }
        ipcpClient.setConfig(svcId, operIds);
        String[] inputSwsmKey = {"steerWhlLeBtn"};
        boolean result = ipcpClient.setSWSMSignal(svcId, operId, inputSwsmKey, value);
        Assert.assertTrue(result);
    }

}
